package com.bawei.module_sign.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_sign.R
import com.bawei.module_sign.model.SignModelItem
import com.bawei.module_sign.model.TYPE_ABSENCE
import com.bawei.module_sign.model.TYPE_ALL
import com.bawei.module_sign.model.TYPE_LATE
import com.bawei.module_sign.model.TYPE_LEAVE_EARLY
import com.bawei.module_sign.model.TYPE_NORMAL

class SignAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val diffCallback = object : DiffUtil.ItemCallback<SignModelItem>() {
        override fun areItemsTheSame(oldItem: SignModelItem, newItem: SignModelItem): Boolean {
            return oldItem.id == newItem.id // 根据唯一标识判断 item 是否相同
        }

        override fun areContentsTheSame(oldItem: SignModelItem, newItem: SignModelItem): Boolean {
            return oldItem == newItem // 判断 item 内容是否相同，数据类重写了 equals 可直接用
        }
    }

    fun submitList(newData : List<SignModelItem>) = differ.submitList(newData)

    private val differ = AsyncListDiffer(this,diffCallback)

    override fun getItemViewType(position: Int): Int {
        val item = differ.currentList[position]
        return when(item.status){
            "全部" -> TYPE_ALL
            "正常" -> TYPE_NORMAL
            "迟到" -> TYPE_LATE
            "早退" -> TYPE_LEAVE_EARLY
            "缺勤" -> TYPE_ABSENCE
            else -> TYPE_ALL
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when(viewType){
            TYPE_ALL -> EmptyViewHolder(inflater.inflate(R.layout.item_sign, parent, false))
            TYPE_NORMAL -> EmptyViewHolder(inflater.inflate(R.layout.item_sign, parent, false))
            TYPE_LATE -> EmptyViewHolder(inflater.inflate(R.layout.item_sign, parent, false))
            TYPE_LEAVE_EARLY -> EmptyViewHolder(inflater.inflate(R.layout.item_sign, parent, false))
            else -> {
                EmptyViewHolder(inflater.inflate(R.layout.item_sign, parent, false))
            }
        }
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = differ.currentList[position]
        when(holder){
            is EmptyViewHolder -> holder.bind(item)
        }
    }

    class EmptyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvSignTime = itemView.findViewById<TextView>(R.id.sign_time)
        private val tvSignStatue = itemView.findViewById<TextView>(R.id.sign_status)
        private val tvSignCheck = itemView.findViewById<TextView>(R.id.sign_check)
        private val tvSignLocation = itemView.findViewById<TextView>(R.id.sign_location)

        fun bind(item: SignModelItem) {
            tvSignTime.text = item.workDate ?: "2025/02/06"
            tvSignStatue.text = item.status ?: "正常"

            tvSignCheck.text = when(item.status) {
                "正常" -> "上班：${item.morningCheckIn}\t下班：${item.afternoonCheckIn}"
                "迟到" -> "上班：${item.morningCheckIn}\t下班：${item.afternoonCheckIn}"
                "早退" -> "上班：${item.morningCheckIn}\t下班：${item.afternoonCheckIn}"
                "缺勤" -> "上班：${item.morningCheckIn}\t下班：${item.afternoonCheckIn}"
                else -> "上班：${item.morningCheckIn}\t下班：${item.afternoonCheckIn}"
            }
            tvSignLocation.text = item.location ?: "放大法"
            when (item.status) {
                "正常" -> {
                    tvSignStatue.text = "正常"
                    tvSignStatue.setTextColor(
                        ContextCompat.getColor(
                            itemView.context,
                            R.color.status_normal
                        )
                    )
                }

                "迟到" -> {
                    tvSignStatue.text = "迟到"
                    tvSignStatue.setTextColor(
                        ContextCompat.getColor(
                            itemView.context,
                            R.color.status_late
                        )
                    )
                }

                "早退" -> {
                    tvSignStatue.text = "早退"
                    tvSignStatue.setTextColor(
                        ContextCompat.getColor(
                            itemView.context,
                            R.color.status_leave_early
                        )
                    )
                }

                "缺勤" -> {
                    tvSignStatue.text = "缺勤"
                    tvSignStatue.setTextColor(
                        ContextCompat.getColor(
                            itemView.context,
                            R.color.status_absence
                        )
                    )
                }
            }
            val bgRes = when (item.status) {
                "正常" -> R.drawable.normal_bg // 对应绿色圆角背景
                "迟到" -> R.drawable.late_bg   // 对应橙色圆角背景
                "早退" -> R.drawable.leave_early_bg // 对应蓝色圆角背景
                "缺勤" -> R.drawable.absence_bg // 对应红色圆角背景
                else -> 0
            }
            if (bgRes != 0) {
                tvSignStatue.background = ContextCompat.getDrawable(itemView.context, bgRes)
            } else {
                tvSignStatue.background = null
            }
        }
    }

}